<template>
  <div>
    <a-textarea v-model="searchForm.sql" :auto-size="{ minRows: 5, maxRows: 10 }" style="width: 100%" />
    <div>
      <a-button @click="fetchList">查询</a-button>
      <a-button @click="toexportSearch">导出</a-button>
    </div>
    <a-table
      :scroll="{ x: true }"
      :columns="recordColumns"
      :data-source="records"
      rowKey="id"
      bordered
      :pagination="false"
      :customRow="customRow"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
    >
    </a-table>
    <a-pagination
      show-size-changer
      :default-current="1"
      :total="searchForm.total"
      @change="pageChange"
      @showSizeChange="onShowSizeChange"
      :show-total="(total) => `共 ${total} 条记录`"
    />
  </div>
</template>
<script>
import { postAction,postDownFile } from '@/api/manage'
import { TableMixin } from '@/mixins/TableMixin'
import { UtilsMixin } from '@/mixins/UtilsMixin'
export default {
  mixins: [TableMixin, UtilsMixin],
  components: {},
  data() {
    return {
      urls: {
        search: '/common/search',
        exportSearch: '/common/exportSearch',
      },
    }
  },
  computed: {
    recordColumns() {
      if (this.records && this.records.length > 0) {
        let temp = []
        for (var key in this.records[0]) {
          temp.push(this.makeColumn(key))
        }
        return temp
      }
      return []
    },
  },
  methods: {
    fetchList() {
      if (this.searchForm.sql && this.searchForm.sql.trim()) {
        postAction(this.urls.search, {
          sql: encodeURI(this.searchForm.sql),
          pageNo: this.searchForm.pageN,
          pageSize: this.searchForm.pageSize,
        }).then((res) => {
          if (res.success) {
            this.records = res.result.records
            this.searchForm.total = res.result.total
          }
        })
      }
    },
    toexportSearch() {
      let url = process.env.VUE_APP_API_BASE_URL + this.urls.exportSearch + '?1=1'
      postDownFile(url, this.searchForm).then((data) => {
        this.loading = false
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', '导出列表.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) //下载完成移除元素
          window.URL.revokeObjectURL(url) //释放掉blob对象
        }
      })
    },
    makeColumn(key) {
      let column = {
        title: key,
        dataIndex: key,
        align: 'center',
        ellipsis: true,
        width: '150px',
        customCell: () => {
          return this.customCellRender(100)
        },
        customRender: (text, record) => {
          return this.tooltipRender(text, record)
        },
      }
      return column
    },
  },
  mounted() {},
}
</script>
<style scoped>
/deep/.ant-table-thead > tr > th {
  white-space: nowrap;
}
</style>
